package com.panshi.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.panshi.api.UserApi;
import com.panshi.vo.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/*
* 登录验证显示
* */
public class LoginVerifyShowPage {

    public Top showPage(UserApi userApi,HttpSession httpSession){
        Integer userId = (Integer) httpSession.getAttribute(httpSession.getId());
        Top top=new Top();

        if(userId != null){
            Object userMemberInfo = userApi.getUserInfo(userId.longValue()).getData().get("userMemberInfo");
            UserDetailResult result = JSON.parseObject(userMemberInfo.toString(), UserDetailResult.class);

            Object data = userApi.getNoReadCount(userId).getData().get("data");
            List<UserNoticeRead> userNoticeReads = JSONArray.parseArray(data.toString(), UserNoticeRead.class);
            int num=0;
            if(userNoticeReads.size() == 0){
                userNoticeReads = null;
            }else{
                for (UserNoticeRead userNoticeRead: userNoticeReads) {
                    num += userNoticeRead.getCount();
                }
            }
            top.setList(userNoticeReads);
            top.setNum(num);
            top.setUserMember(result.getUserMember());
        }
        return top;
    }
}
